package service

import (
	"bilibi.com/comment/model"
	"context"
	"fmt"
	log "github.com/sirupsen/logrus"
)

func (s *Service) AddComment(c context.Context, req *model.AddComment) (err error) {
	// return ecode.CommentFilterErr
	return s.dao.AddComment(c, req)
}

func (s *Service) CommentQuery(c context.Context, req *model.ArgQueryComment) (res []*model.Comment, err error) {
	return s.dao.CommentQuery(c, req)
}

func (s *Service) CommentList(c context.Context, req *model.ArgList) (res *model.CommentPagination, err error) {
	return s.dao.CommentList(c, req)
}

func (s *Service) Comment(c context.Context, id int64) (res *model.Comment, err error) {
	var (
		ok      bool
		comment *model.Comment
	)
	if ok, err = s.dao.ExpireComment(c, id); err != nil {
		log.Error("s.dao.ExpireComment(%d) error(%v)", id, err)
		return
	}
	if ok { // cache exist
		fmt.Printf("cache hit")
		comment, err = s.dao.GetCommentCache(c, id)
		return comment, err
	} else {
		fmt.Printf("cache miss")
		comment, err = s.dao.Comment(c, id)
		_ = s.dao.AddCommentCache(c, id, comment)
		return comment, err
	}

}

func (s *Service) UpdateComment(c context.Context, req *model.UpdateComment) error {
	return s.dao.UpdateComment(c, req)
}

func (s *Service) DeleteComment(c context.Context, id int64) error {
	return s.dao.DeleteComment(c, id)
}
